<template>
  <div>
    <div>
      <el-form
        ref="formRel"
        :model="form"
        label-width="120px"
        style="max-width: 800px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="销售单号">
              <el-input v-model="form.code" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="费用">
              <el-input v-model="form.price" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称">
              <el-input v-model="form.cname" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目的地">
              <el-input v-model="form.end" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="产品">
              <el-input v-model="form.product" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量">
              <el-input v-model="form.num" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="抵达日期">
          <el-date-picker
            v-model="form.endtime"
            type="date"
            format="YYYY-MM-DD"
            placeholder="选择日期"
          />
        </el-form-item>

        <div>
          <el-button type="primary" @click="savefrom">保存</el-button>
          <el-button type="default" @click="cls">取消</el-button>
        </div>

        <!-- Add other fields similarly -->
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from "vue";
import { ElMessage, dayjs } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import { getcode, save } from "@/api/order.js";
import { getUser } from "@/utils/session.js";
const router = useRouter();
const rou = useRoute();
const form = ref({
  code: "",
  endtime: "",
  cname: "",
  price: "",
  product: "",
  num: "",
  status: "",
  saleid: "",
  end: ""
  // Initialize other fields similarly
});

async function getcodeinit() {
  let codeget = await getcode();
  console.log(codeget);
  form.value.code = codeget.data;
}

async function savefrom() {
  let endtimeformat = dayjs(form.value.endtime).format("YYYY-MM-DD");
  form.value.endtime = endtimeformat;
  console.log(form.value);
  //验证规则 为false时，不会触发表单提交
  let res = await save(form.value);
  if (res.code === 200) {
    ElMessage({
      message: "保存成功",
      type: "success"
    });

    //刷新部门列表
    router.push("/order/index");
    // cls();
  }
}
function cls() {
  emits("close");
}

onBeforeMount(() => {
  getcodeinit();
  // form.value.cname = rou.message.name;
  let user = getUser();
  console.log(user);

  form.value.saleid = user.id;
  // form.saleid=user.
});
</script>
